package com.song.web;

import com.song.bean.Users;
import com.song.service.impl.UsersServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**登录验证
 *
 * @author song*/
@WebServlet(urlPatterns ="/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("123");

        //获取用户信息
        String userName = req.getParameter("username");
        String passWord = req.getParameter("password");
        System.out.println(userName);


        //调用service
        UsersServiceImpl usersService = new UsersServiceImpl();
        Users user = usersService.getUser(userName, passWord);

        if (user != null) {
            System.out.println("成功");
            //登录成功，session存值用户信息，跳转
            req.getSession().setAttribute("user",user);
            //req.setAttribute("user",user);
            req.getRequestDispatcher("index.jsp").forward(req,resp);
            //resp.sendRedirect("index.jsp");

        }else {
            //登录不成功，返回登录页面
            System.out.println("失败");
            PrintWriter writer = resp.getWriter();
            writer.println("<script>location.href='login.jsp';alert('用户名或密码不正确');</script>");
        }

    }
}
